﻿using STMS.DAL;
using STMS.Models.DModels;
using STMS.Models.VModels;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STMS.BLL
{
    public class StoreRegionBLL
    {

        private StoreRegionDAL srDAL = new StoreRegionDAL();
        private ViewStoreRegionDAL vsrDAL = new ViewStoreRegionDAL();
        /// <summary>
        /// 添加仓库分区
        /// </summary>
        /// <param name="srInfo"></param>
        /// <returns></returns>
        public bool AddStoreRegion(StoreRegionInfo srInfo)
        {
            return srDAL.AddStoreRegion(srInfo, 0) > 0;
        }

        public int AddStoreRegionWithId(StoreRegionInfo srInfo)
        {
            return srDAL.AddStoreRegion(srInfo, 1);
        }

        /// <summary>
        /// 修改仓库分区信息
        /// </summary>
        /// <param name="srInfo"></param>
        /// <returns></returns>
        public bool UpdateStoreRegion(StoreRegionInfo srInfo, int oldStoreId)
        {
            return srDAL.UpdateStoreRegion(srInfo, oldStoreId);
        }
        public bool UpdateStoreRegion(StoreRegionInfo srInfo)
        {
            return srDAL.UpdateStoreRegion(srInfo, 0);
        }

        /// <summary>
        /// 获取指定的分区信息
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public StoreRegionInfo GetRegion(int regionId)
        {
            return srDAL.GetRegion(regionId);
        }

        /// <summary>
        /// 获取指定仓库下的分区
        /// </summary>
        /// <param name="storeId"></param>
        /// <returns></returns>
        public List<StoreRegionInfo> GetRegionsByStoreId(int storeId)
        {
            return srDAL.GetRegionsByStoreId(storeId);
        }

        /// <summary>
        /// 检查指定分区是否已存储产品
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public bool IsRegionAddProducts(int regionId)
        {
            bool bl = false;
            DataTable dt = srDAL.GetRegionProductRecords(regionId);
            if(dt.Rows[0][0].ToString()!="")
            {
                int count1 = (int)dt.Rows[0][0];
                if (count1 == 0)
                    bl = false;
                else if (count1 > 0)
                    bl = true;
            }
            return bl;
        }

        /// <summary>
        /// 条件查询仓库分区列表
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="temperState"></param>
        /// <param name="keywords"></param>
        /// <param name="showDel"></param>
        /// <returns></returns>
        public List<ViewStoreRegionInfo> GetStoreRegionList(int storeId, int temperState, string keywords, bool showDel)
        {
            int isDeleted = showDel ? 1 : 0;
            return vsrDAL.GetStoreRegionList(storeId, temperState, keywords, isDeleted);
        }

        /// <summary>
        /// 判断分区名称是否已存在
        /// </summary>
        /// <param name="regionName"></param>
        /// <returns></returns>
        public bool ExistsSRegion(string regionName)
        {
            return srDAL.ExistsByName("SRegionName", regionName);
        }


        /// <summary>
        /// 仓库分区删除  真删除 
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public bool DeleteStoreRegion(int regionId)
        {
            return srDAL.Delete(regionId, 1, 2);
        }

        /// <summary>
        /// 逻辑删除仓库分区信息  假删除
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public int LogicDeleteStoreRegion(int regionId)
        {
            int re = 0;
            if (IsRegionAddProducts(regionId))
                re = 2;//表示已存储产品
            else
            {
                bool bl = srDAL.Delete(regionId, 0, 1);
                if (bl)
                    re = 1;//删除成功   
            }
            return re;
        }

        public string LogicDeleteStoreRegion(List<int> regionIds)
        {
            string reStr = "";
            foreach (int id in regionIds)
            {
                if (IsRegionAddProducts(id))
                {
                    if (reStr != "") reStr += ",";
                    //表示已存储了产品
                    reStr += id.ToString();
                }
            }
            if (reStr == "")
                reStr = srDAL.DeleteList(regionIds, 0, 1) ? "" : "0";
            return reStr;
        }

        /// <summary>
        /// 恢复仓库分区信息
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public bool RecoverStoreRegion(int regionId)
        {
            return srDAL.Delete(regionId, 0, 0);
        }

    }
}
